/*
 * This file is part of the Try! Engine project.
 *
 * Copyright 2010-2011 Emanuele Bertoldi. All rights reserved.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *
 * You should have received a copy of the modified BSD License along with this
 * program. If not, see <http://www.opensource.org/licenses/bsd-license.php>
 */

#include "projectmapper.h"
#include <projectmodellist.h>

using namespace Try;

ProjectMapper::ProjectMapper(Project* project, QObject* parent)
    :   DataMapper(parent),
        m_project(0)
{
    QObject::connect(ProjectModelList::instance(), SIGNAL(projectLoaded(Project*)), this, SLOT(setProject(Project*)));
    QObject::connect(ProjectModelList::instance(), SIGNAL(projectChanged(Project*)), this, SLOT(onProjectChanged(Project*)));
    QObject::connect(ProjectModelList::instance(), SIGNAL(projectClosed(Project*)), this, SLOT(onProjectClosed(Project*)));
    QObject::connect(this, SIGNAL(propertyChanged(QString)), this, SLOT(onPropertyChanged(QString)));

    this->setProject(project);
}

ProjectMapper::~ProjectMapper()
{
}

void ProjectMapper::setProject(Project* project)
{
    if (project != m_project)
    {
        m_project = project;

        this->refresh();
    }
}

void ProjectMapper::refresh()
{
    QVariant name;
    QVariant description;
    QVariant author;

    if (m_project)
    {
        name.setValue<QString>(m_project->name());
        description.setValue<QString>(m_project->description());
        author.setValue<QString>(m_project->author());
    }

    this->setProperty("Name", name);
    this->setProperty("Description", description);
    this->setProperty("Author", author);
}

void ProjectMapper::onProjectChanged(Project* project)
{
    if (project && project == m_project)
        this->refresh();
}

void ProjectMapper::onProjectClosed(Project* project)
{
    if (project == m_project)
        this->setProject(0);
}

void ProjectMapper::onPropertyChanged(const QString& name)
{
    if (m_project)
    {
        if (name == tr("Name"))
            m_project->setName(this->property(name.toUtf8().data()).toString());

        else if (name == tr("Description"))
            m_project->setDescription(this->property(name.toUtf8().data()).toString());

        else if (name == tr("Author"))
            m_project->setAuthor(this->property(name.toUtf8().data()).toString());
    }
}
